﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OfficeBusiness.Model;
using System.Collections.ObjectModel;
using System.Windows.Input;
using System.Windows;

namespace OfficeBusiness.ViewModel
{
    public class ProjectViewModel : ViewModelBase
    {
        private IBusinessModel businessModel;
        private ObservableCollection<ViewModel.Project> projectsData;
        private Project selectedItem;
        private ICommand createCommand;
        private ICommand refreshCommand;
        private ICommand deleteRowCommand;
        private ICommand updateCommand;

        public ProjectViewModel(IBusinessModel businessModel)
        {
            this.businessModel = businessModel;
            this.businessModel.ProjectsSelectedEvent += new EventHandler(ProjectSelected);
        }

        private void ProjectSelected(object projectVisibility, EventArgs e)
        {
            this.ProjectData.Clear();
            this.GetProjectDetails();
        }

        private void GetProjectDetails()
        {
            var projList = this.businessModel.GetProjectsDetails();
            foreach (var empItem in projList)
            {
                this.ProjectData.Add(empItem);
            }
        }

        public ObservableCollection<ViewModel.Project> ProjectData
        {
            get
            {
                if (this.projectsData == null)
                {
                    this.projectsData = new ObservableCollection<Project>();
                }

                return this.projectsData;
            }
        }

        public Project SelectedItem
        {
            get
            {
                return this.selectedItem;
            }

            set
            {
                this.selectedItem = value;
                OnPropertyChanged("SelectedItem");
            }
        }

        public ICommand CreateCommand
        {
            get
            {
                if (this.createCommand == null)
                {
                    this.createCommand = new RelayCommand(param => this.CreateRow(), param => true);
                }

                return this.createCommand;
            }
        }

        public ICommand RefreshCommand
        {
            get
            {
                if (this.refreshCommand == null)
                {
                    this.refreshCommand = new RelayCommand(param => this.RefreshTable(), param => true);
                }

                return this.refreshCommand;
            }
        }

        public ICommand DeleteRowCommand
        {
            get
            {
                if (this.deleteRowCommand == null)
                {
                    this.deleteRowCommand = new RelayCommand(param => this.DeleteRow(), param => true);
                }

                return this.deleteRowCommand;
            }
        }

        public ICommand UpdateCommand
        {
            get
            {
                if (this.updateCommand == null)
                {
                    this.updateCommand = new RelayCommand(param => this.Update(), param => true);
                }

                return this.updateCommand;
            }
        }

        private void Update()
        {
            var projList = from e in this.ProjectData select e;
            bool updateStatus = this.businessModel.UpdateRows<ViewModel.Project>(projList.ToList());
            if (!updateStatus)
            {
                MessageBox.Show("Update failed");
                this.RefreshTable();
            }
        }

        private void DeleteRow()
        {
            if (this.SelectedItem != null)
            {
                bool deleteStatus = this.businessModel.DeleteSelectedRow<ViewModel.Project>(this.SelectedItem);
                if (!deleteStatus)
                {
                    MessageBox.Show("Delete failed");
                }
                else
                {
                    this.ProjectData.Remove(this.SelectedItem);
                }
            }
            else
            {
                MessageBox.Show("Please select a row");
            }
        }

        private void CreateRow()
        {
            var projsList = this.businessModel.GetProjectsDetails();
            var newItems = from itms in this.ProjectData where !(from exItems in projsList select exItems.ProjectNumber).Contains(itms.ProjectNumber) select itms;
            bool createStatus = this.businessModel.InsetRows<ViewModel.Project>(newItems.ToList());
            if (!createStatus)
            {
                MessageBox.Show("Create failed");
                this.RefreshTable();
            }
        }

        private void RefreshTable()
        {
            this.ProjectData.Clear();
            this.GetProjectDetails();
        }
    }
}
